package cn.csc.module.product.controller.admin.attrcategory;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.product.controller.admin.attrcategory.vo.*;
import cn.csc.module.product.convert.attrcategory.AttrCategoryConvert;
import cn.csc.module.product.dal.dataobject.attrcategory.AttrCategoryDO;
import cn.csc.module.product.service.attrcategory.AttrCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 属性与分类关联信息")
@RestController
@RequestMapping("/product/attr-category")
@Validated
public class AttrCategoryController {

    @Resource
    private AttrCategoryService attrCategoryService;

    @PostMapping("/create")
    @ApiOperation("创建属性与分类关联信息")
    @PreAuthorize("@ss.hasPermission('product:attr-category:create')")
    public CommonResult<BigInteger> createAttrCategory(@Valid @RequestBody AttrCategoryCreateReqVO createReqVO) {
        return success(attrCategoryService.createAttrCategory(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新属性与分类关联信息")
    @PreAuthorize("@ss.hasPermission('product:attr-category:update')")
    public CommonResult<Boolean> updateAttrCategory(@Valid @RequestBody AttrCategoryUpdateReqVO updateReqVO) {
        attrCategoryService.updateAttrCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除属性与分类关联信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:attr-category:delete')")
    public CommonResult<Boolean> deleteAttrCategory(@RequestParam("id") BigInteger id) {
        attrCategoryService.deleteAttrCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得属性与分类关联信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:attr-category:query')")
    public CommonResult<AttrCategoryRespVO> getAttrCategory(@RequestParam("id") BigInteger id) {
        AttrCategoryDO attrCategory = attrCategoryService.getAttrCategory(id);
        return success(AttrCategoryConvert.INSTANCE.convert(attrCategory));
    }

    @GetMapping("/list")
    @ApiOperation("获得属性与分类关联信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:attr-category:query')")
    public CommonResult<List<AttrCategoryRespVO>> getAttrCategoryList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<AttrCategoryDO> list = attrCategoryService.getAttrCategoryList(ids);
        return success(AttrCategoryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得属性与分类关联信息分页")
    @PreAuthorize("@ss.hasPermission('product:attr-category:query')")
    public CommonResult<PageResult<AttrCategoryRespVO>> getAttrCategoryPage(@Valid AttrCategoryPageReqVO pageVO) {
        PageResult<AttrCategoryDO> pageResult = attrCategoryService.getAttrCategoryPage(pageVO);
        return success(AttrCategoryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出属性与分类关联信息 Excel")
    @PreAuthorize("@ss.hasPermission('product:attr-category:export')")
    @OperateLog(type = EXPORT)
    public void exportAttrCategoryExcel(@Valid AttrCategoryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AttrCategoryDO> list = attrCategoryService.getAttrCategoryList(exportReqVO);
        // 导出 Excel
        List<AttrCategoryExcelVO> datas = AttrCategoryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "属性与分类关联信息.xls", "数据", AttrCategoryExcelVO.class, datas);
    }

}
